package com.zmn.oms.dubbo.impl.normal.master;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.dio.normal.visit.OrderVisitDIO;
import com.zmn.oms.common.dro.visit.OrderVisitResultDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.normal.master.MasterVisitRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.visit.OrderVisitDTO;
import com.zmn.oms.model.dto.work.modify.OrderMultipleVisitDTO;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkMultipleVisitBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述：工程师上门
 *
 * @author wangxiaokun
 * @since 2021/10/11 15:18
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class MasterVisitRemoteServiceImpl implements MasterVisitRemoteService {

    @Autowired
    private OrderWorkMultipleVisitBService orderWorkMultipleVisitBService;

    /**
     * 新增多次上门
     * @param orderVisitDIO
     * @return
     */
    @Override
    public ResponseDTO<OrderVisitResultDRO> addOrderWorkMultipleVisit(OrderVisitDIO orderVisitDIO) {
        log.info("新增多次上门入参：【{}】", JSON.toJSONString(orderVisitDIO));
        try {
            OrderMultipleVisitDTO dto = BeanMapper.map(orderVisitDIO, OrderMultipleVisitDTO.class);
            dto.setOperator(orderVisitDIO.getMasterName());
            dto.setOperatorId((long) orderVisitDIO.getMasterId());
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            dto.setOperateTime(DateUtil.getNow());
            OrderVisitDTO orderVisitDTO = orderWorkMultipleVisitBService.addOrderMultipleVisit(dto);
            OrderVisitResultDRO dro = BeanMapper.map(orderVisitDTO, OrderVisitResultDRO.class);
            return ResponseDTO.success(dro);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增多次上门失败", e);
            return ResponseDTO.fail("新增多次上门失败，请稍后重试");
        }
    }

    /**
     * 再次出发
     * @param orderVisitDIO
     * @return
     */
    @Override
    public ResponseDTO againSetOut(OrderVisitDIO orderVisitDIO) {
        log.info("再次出发入参：【{}】", JSON.toJSONString(orderVisitDIO));
        try {
            OrderMultipleVisitDTO dto = BeanMapper.map(orderVisitDIO, OrderMultipleVisitDTO.class);
            dto.setOperator(orderVisitDIO.getMasterName());
            dto.setOperatorId((long) orderVisitDIO.getMasterId());
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            dto.setOperateTime(DateUtil.getNow());
            orderWorkMultipleVisitBService.againSetOut(dto);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("再次出发失败", e);
            return ResponseDTO.fail("再次出发失败，请稍后重试");
        }
    }

    /**
     * 再次上门
     * @param orderVisitDIO
     * @return
     */
    @Override
    public ResponseDTO againVisit(OrderVisitDIO orderVisitDIO) {
        log.info("再次上门入参：【{}】", JSON.toJSONString(orderVisitDIO));
        try {
            OrderMultipleVisitDTO dto = BeanMapper.map(orderVisitDIO, OrderMultipleVisitDTO.class);
            dto.setOperator(orderVisitDIO.getMasterName());
            dto.setOperatorId((long) orderVisitDIO.getMasterId());
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            dto.setOperateTime(DateUtil.getNow());
            orderWorkMultipleVisitBService.againVisit(dto);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("再次上门失败", e);
            return ResponseDTO.fail("再次上门失败，请稍后重试");
        }
    }

    /**
     * 取消再次上门
     * @param orderVisitDIO
     * @return
     */
    @Override
    public ResponseDTO cancelAgainVisit(OrderVisitDIO orderVisitDIO) {
        log.info("取消再次上门入参：【{}】", JSON.toJSONString(orderVisitDIO));
        try {
            OrderMultipleVisitDTO dto = BeanMapper.map(orderVisitDIO, OrderMultipleVisitDTO.class);
            dto.setOperator(orderVisitDIO.getMasterName());
            dto.setOperatorId((long) orderVisitDIO.getMasterId());
            dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            dto.setOperateTime(DateUtil.getNow());
            orderWorkMultipleVisitBService.engineerCancelAgainVisit(dto);
            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("取消再次上门失败", e);
            return ResponseDTO.fail("取消再次上门失败，请稍后重试");
        }
    }
}
